home *** CD-ROM | disk | FTP | other *** search
/ Aminet 6 / Aminet 6 - June 1995.iso / Aminet / gfx / 3d / irit50src.lha / irit5 / grapdrvs / wnt_ogl.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-27  |  22.9 KB  |  710 lines

  1. /*****************************************************************************
  2. *   A Windows NT driver - Open GL graphics calls.                 *
  3. *   The Pallete code was taken from the gengl open gl example in         *
  4. * /mstools/samples/opengl/demos/gengl. Ugly staff to create default pallete! *
  5. *                                         *
  6. * Written by:  Gershon Elber                Ver 0.1, June 1994.  *
  7. *****************************************************************************/
  8.  
  9. #include <windows.h>
  10. #include <gl/gl.h>
  11. #include <gl/glaux.h>
  12.  
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include <math.h>
  16. #include "irit_sm.h"
  17. #include "genmat.h"
  18. #include "iritprsr.h"
  19. #include "allocate.h"
  20. #include "attribut.h"
  21. #include "cagd_lib.h"
  22. #include "symb_lib.h"
  23. #include "iritgrap.h"
  24. #include "irit_soc.h"
  25. #include "wntdrvs.h"
  26.  
  27. /* Disable warnings for double <-> float conversions. */
  28. #pragma warning(disable : 4244)
  29.  
  30. static HPALETTE ghpalOld,
  31.     ghPalette = (HPALETTE) 0;
  32.  
  33. static unsigned char ComponentFromIndex(int i, UINT NBits, UINT Shift);
  34. static void CreateRGBPalette(HDC hDC);
  35. static BOOL bSetupPixelFormat(HDC hDC);
  36. static void SetColorIndex(int c);
  37. static void SetColorRGB(int Color[3]);
  38.  
  39. /*****************************************************************************
  40. * DESCRIPTION:                                                               *
  41. * The following three ugly routines (ComponentFromIndex, CreateRGBPalette    *
  42. * and bSetupPixelFormat) were copied verbatim from the gengl example in      *
  43. * /mstools/samples/opengl/demos/gengl. All they do is to figure out the      *
  44. * color capability of the hardware and create a reasonablepallete for 8 bit  *
  45. * per pixel devices to emulate true 24 bit colors. This should have been a   *
  46. * service provided by the OS.                             *
  47. *                                                                            *
  48. * PARAMETERS:                                                                *
  49. *   i, NBits, Shift:   Dont ask me. I am not responsible for this mess         *
  50. *                                                                            *
  51. * RETURN VALUE:                                                              *
  52. *   unsigned:          Same!                                                 *
  53. *****************************************************************************/
  54. static unsigned char ComponentFromIndex(int i, UINT NBits, UINT Shift)
  55. {
  56.     static unsigned char
  57.     ThreeTo8[8] = {
  58.         0, 0111>>1, 0222>>1, 0333>>1, 0444>>1, 0555>>1, 0666>>1, 0377
  59.     },
  60.     TwoTo8[4] = {
  61.         0, 0x55, 0xaa, 0xff
  62.     },
  63.     OneTo8[2] = {
  64.         0, 255
  65.     };
  66.     unsigned char
  67.     Val = (unsigned char) (i >> Shift);
  68.  
  69.     switch (NBits) {
  70.     case 1:
  71.         Val &= 0x1;
  72.         return OneTo8[Val];
  73.     case 2:
  74.         Val &= 0x3;
  75.         return TwoTo8[Val];
  76.     case 3:
  77.         Val &= 0x7;
  78.         return ThreeTo8[Val];
  79.     default:
  80.         return 0;
  81.     }
  82. }
  83.  
  84. static void CreateRGBPalette(HDC hDC)
  85. {
  86.     PIXELFORMATDESCRIPTOR pfd;
  87.     LOGPALETTE *pPal;
  88.     int n, i;
  89.     static int defaultOverride[13] = {
  90.     0, 3, 24, 27, 64, 67, 88, 173, 181, 236, 247, 164, 91
  91.     };
  92.     static PALETTEENTRY defaultPalEntry[20] = {
  93.     { 0,   0,   0,    0 },
  94.     { 0x80,0,   0,    0 },
  95.     { 0,   0x80,0,    0 },
  96.     { 0x80,0x80,0,    0 },
  97.     { 0,   0,   0x80, 0 },
  98.     { 0x80,0,   0x80, 0 },
  99.     { 0,   0x80,0x80, 0 },
  100.     { 0xC0,0xC0,0xC0, 0 },
  101.  
  102.     { 192, 220, 192,  0 },
  103.     { 166, 202, 240,  0 },
  104.     { 255, 251, 240,  0 },
  105.     { 160, 160, 164,  0 },
  106.  
  107.     { 0x80,0x80,0x80, 0 },
  108.     { 0xFF,0,   0,    0 },
  109.     { 0,   0xFF,0,    0 },
  110.     { 0xFF,0xFF,0,    0 },
  111.     { 0,   0,   0xFF, 0 },
  112.     { 0xFF,0,   0xFF, 0 },
  113.     { 0,   0xFF,0xFF, 0 },
  114.     { 0xFF,0xFF,0xFF, 0 }
  115.     };
  116.  
  117.     n = GetPixelFormat(hDC);
  118.     DescribePixelFormat(hDC, n, sizeof(PIXELFORMATDESCRIPTOR), &pfd);
  119.  
  120.     if (pfd.dwFlags & PFD_NEED_PALETTE) {
  121.         n = 1 << pfd.cColorBits;
  122.         pPal = (PLOGPALETTE)LocalAlloc(LMEM_FIXED, sizeof(LOGPALETTE) +
  123.                 n * sizeof(PALETTEENTRY));
  124.         pPal -> palVersion = 0x300;
  125.         pPal -> palNumEntries = n;
  126.         for (i=0; i<n; i++) {
  127.             pPal -> palPalEntry[i].peRed =
  128.                     ComponentFromIndex(i, pfd.cRedBits, pfd.cRedShift);
  129.             pPal -> palPalEntry[i].peGreen =
  130.                     ComponentFromIndex(i, pfd.cGreenBits, pfd.cGreenShift);
  131.             pPal -> palPalEntry[i].peBlue =
  132.                     ComponentFromIndex(i, pfd.cBlueBits, pfd.cBlueShift);
  133.             pPal -> palPalEntry[i].peFlags = 0;
  134.         }
  135.  
  136.         /* Fix up the palette to include the default GDI palette. */
  137.         if ((pfd.cColorBits == 8)                           &&
  138.             (pfd.cRedBits   == 3) && (pfd.cRedShift   == 0) &&
  139.             (pfd.cGreenBits == 3) && (pfd.cGreenShift == 3) &&
  140.             (pfd.cBlueBits  == 2) && (pfd.cBlueShift  == 6)) {
  141.             for (i = 1 ; i <= 12 ; i++)
  142.                 pPal -> palPalEntry[defaultOverride[i]] = defaultPalEntry[i];
  143.         }
  144.  
  145.         ghPalette = CreatePalette(pPal);
  146.         LocalFree(pPal);
  147.  
  148.         ghpalOld = SelectPalette(hDC, ghPalette, FALSE);
  149.         n = RealizePalette(hDC);
  150.     }
  151. }
  152.  
  153. static BOOL bSetupPixelFormat(HDC hDC)
  154. {
  155.     static PIXELFORMATDESCRIPTOR pfd = {
  156.     sizeof(PIXELFORMATDESCRIPTOR),    /* Size of this pfd. */
  157.     1,                /* Version number. */
  158.     PFD_DRAW_TO_WINDOW |        /* Support window. */
  159.       PFD_SUPPORT_OPENGL |        /* Support OpenGL. */
  160.       PFD_DOUBLEBUFFER,        /* Double buffered. */
  161.     PFD_TYPE_RGBA,            /* RGBA type. */
  162.     24,                /* 24-bit color depth. */
  163.     0, 0, 0, 0, 0, 0,        /* Color bits ignored. */
  164.     0,                /* No alpha buffer. */
  165.     0,                /* Shift bit ignored. */
  166.     0,                /* No accumulation buffer. */
  167.     0, 0, 0, 0,             /* Accum bits ignored. */
  168.     32,                /* 32-bit z-buffer. */
  169.     0,                /* No stencil buffer. */
  170.     0,                /* No auxiliary buffer. */
  171.     PFD_MAIN_PLANE,            /* Main layer. */
  172.     0,                /* Reserved. */
  173.     0, 0, 0                /* Layer masks ignored. */
  174.     };
  175.     int PixelFormat, i;
  176.  
  177.     if ((PixelFormat = ChoosePixelFormat(hDC, &pfd)) == 0) {
  178.     i = GetLastError();
  179.         MessageBox(NULL, "ChoosePixelFormat failed", "Error", MB_OK);
  180.         return FALSE;
  181.     }
  182.  
  183.     if (SetPixelFormat(hDC, PixelFormat, &pfd) == FALSE) {
  184.     i = GetLastError();
  185.         MessageBox(NULL, "SetPixelFormat failed", "Error", MB_OK);
  186.         return FALSE;
  187.     }
  188.  
  189.     CreateRGBPalette(hDC);
  190.  
  191.     return TRUE;
  192. }
  193.  
  194. /*****************************************************************************
  195. * DESCRIPTION:                                                               M
  196. * Draw a single Point/Vector object using current modes and transformations. M
  197. *                                                                            *
  198. * PARAMETERS:                                                                M
  199. *   PObj:     A point/vector object to draw.                                 M
  200. *                                                                            *
  201. * RETURN VALUE:                                                              M
  202. *   void                                                                     M
  203. *                                                                            *
  204. * KEYWORDS:                                                                  M
  205. *   IGDrawPtVec                                                              M
  206. *****************************************************************************/
  207. void IGDrawPtVec(IPObjectStruct *PObj)
  208. {
  209.     int i;
  210.     PointType Ends[6], Zero;
  211.     RealType
  212.     *Pt = PObj -> U.Pt;
  213.  
  214.     IGSetColorObj(PObj);
  215.  
  216.     for (i = 0; i < 6; i++)
  217.     PT_COPY(Ends[i], Pt);
  218.  
  219.     Ends[0][0] -= IG_POINT_WIDTH;
  220.     Ends[1][0] += IG_POINT_WIDTH;
  221.     Ends[2][1] -= IG_POINT_WIDTH;
  222.     Ends[3][1] += IG_POINT_WIDTH;
  223.     Ends[4][2] -= IG_POINT_WIDTH;
  224.     Ends[5][2] += IG_POINT_WIDTH;
  225.  
  226.     for (i = 0; i < 6; i += 2) {
  227.     glBegin(GL_LINES);
  228.     glVertex3dv(Ends[i]);
  229.     glVertex3dv(Ends[i+1]);
  230.     glEnd();
  231.     }
  232.  
  233.     if (IP_IS_VEC_OBJ(PObj)) {
  234.     glBegin(GL_LINES);
  235.     glVertex3dv(Pt);
  236.     Zero[0] = Zero[1] = Zero[2] = 0.0;
  237.     glVertex3dv(Zero);
  238.     glEnd();
  239.     }
  240. }
  241.  
  242. /*****************************************************************************
  243. * DESCRIPTION:                                                               M
  244. * Draw a single Poly object using current modes and transformations.         M
  245. *                                                                            *
  246. * PARAMETERS:                                                                M
  247. *   PObj:     A poly object to draw.                                         M
  248. *                                                                            *
  249. * RETURN VALUE:                                                              M
  250. *   void                                                                     M
  251. *                                                                            *
  252. * KEYWORDS:                                                                  M
  253. *   IGDrawPoly                                                               M
  254. *****************************************************************************/
  255. void IGDrawPoly(IPObjectStruct *PObj)
  256. {
  257.     IPVertexStruct *V;
  258.     IPPolygonStruct
  259.     *Pl = PObj -> U.Pl;
  260.  
  261.     IGSetColorObj(PObj);
  262.  
  263.     if (IP_IS_POLYLINE_OBJ(PObj)) {
  264.     for (; Pl != NULL; Pl = Pl -> Pnext) {
  265.         glBegin(GL_LINE_STRIP);
  266.         for (V = Pl -> PVertex; V != NULL; V = V -> Pnext)
  267.             glVertex3dv(V -> Coord);
  268.         glEnd();
  269.     }
  270.     }
  271.     else if (IP_IS_POINTLIST_OBJ(PObj)) {
  272.     for (; Pl != NULL; Pl = Pl -> Pnext) {
  273.         for (V = Pl -> PVertex; V != NULL; V = V -> Pnext) {
  274.         int i;
  275.         PointType Ends[6];
  276.         RealType
  277.             *Pt = V -> Coord;
  278.  
  279.         for (i = 0; i < 6; i++)
  280.             PT_COPY(Ends[i], Pt);
  281.  
  282.         Ends[0][0] -= IG_POINT_WIDTH;
  283.         Ends[1][0] += IG_POINT_WIDTH;
  284.         Ends[2][1] -= IG_POINT_WIDTH;
  285.         Ends[3][1] += IG_POINT_WIDTH;
  286.         Ends[4][2] -= IG_POINT_WIDTH;
  287.         Ends[5][2] += IG_POINT_WIDTH;
  288.  
  289.         for (i = 0; i < 6; i += 2) {
  290.             glBegin(GL_LINES);
  291.             glVertex3dv(Ends[i]);
  292.             glVertex3dv(Ends[i+1]);
  293.             glEnd();
  294.         }
  295.         }
  296.     }
  297.     }
  298.     else if (IP_IS_POLYGON_OBJ(PObj)) {
  299.     int i, j,
  300.         NumOfVertices;
  301.     PointType PNormal, VNormal;
  302.  
  303.     for (; Pl != NULL; Pl = Pl -> Pnext) {
  304.         if (IGGlblDrawPNormal) {
  305.         NumOfVertices = 0;
  306.         PNormal[0] = PNormal[1] = PNormal[2] = 0.0;
  307.         }
  308.  
  309.         if (IGGlblDrawSolid) {
  310.         glBegin(GL_POLYGON);
  311.         for (V = Pl -> PVertex; V != NULL; V = V -> Pnext) {
  312.             glNormal3dv(V -> Normal);
  313.             glVertex3dv(V -> Coord);
  314.  
  315.             if (IGGlblDrawPNormal) {
  316.             for (j = 0; j < 3; j++)
  317.                 PNormal[j] += V -> Coord[j];
  318.             NumOfVertices++;
  319.             }
  320.         }
  321.         glEnd();
  322.         }
  323.         else {
  324.         glBegin(GL_LINE_STRIP);
  325.         for (V = Pl -> PVertex; V != NULL; V = V -> Pnext) {
  326.             glVertex3dv(V -> Coord);
  327.             if (IP_IS_INTERNAL_VRTX(V) && !IGGlblDrawInternal) {
  328.             glEnd();
  329.             glBegin(GL_LINE_STRIP);
  330.             }
  331.  
  332.             if (IGGlblDrawPNormal) {
  333.             for (j = 0; j < 3; j++)
  334.                 PNormal[j] += V -> Coord[j];
  335.             NumOfVertices++;
  336.             }
  337.         }
  338.         glVertex3dv(Pl -> PVertex -> Coord);
  339.         glEnd();
  340.         }
  341.  
  342.         if (IGGlblDrawPNormal && IP_HAS_PLANE_POLY(Pl)) {
  343.         glBegin(GL_LINES);
  344.         for (i = 0; i < 3; i++)
  345.             PNormal[i] /= NumOfVertices;
  346.         glVertex3dv(PNormal);
  347.         for (i = 0; i < 3; i++)
  348.             PNormal[i] += Pl -> Plane[i] * IGGlblNormalLen;
  349.         glVertex3dv(PNormal);
  350.         glEnd();
  351.         }
  352.  
  353.         if (IGGlblDrawVNormal) {
  354.         for (V = Pl -> PVertex; V != NULL; V = V -> Pnext) {
  355.             if (IP_HAS_NORMAL_VRTX(V)) {
  356.             for (j = 0; j < 3; j++)
  357.                 VNormal[j] = V -> Coord[j] +
  358.                          V -> Normal[j] * IGGlblNormalLen;
  359.             glBegin(GL_LINES);
  360.             glVertex3dv(V -> Coord);
  361.             glVertex3dv(VNormal);
  362.             glEnd();
  363.             }
  364.         }
  365.         }
  366.     }
  367.     }
  368. }
  369.  
  370. /*****************************************************************************
  371. * DESCRIPTION:                                                               M
  372. * Sets the color of an object according to its color/rgb attributes.         M
  373. *   If object has an RGB attribute it will be used. Otherwise, if the object M
  374. * has a COLOR attribute it will use. Otherwise, WHITE will be used.         M
  375. *                                                                            *
  376. * PARAMETERS:                                                                M
  377. *   PObj:      To set the drawing color to its color.                        M
  378. *                                                                            *
  379. * RETURN VALUE:                                                              M
  380. *   void                                                                     M
  381. *                                                                            *
  382. * KEYWORDS:                                                                  M
  383. *   IGSetColorObj                                                            M
  384. *****************************************************************************/
  385. void IGSetColorObj(IPObjectStruct *PObj)
  386. {
  387.     int c, Color[3];
  388.  
  389.     if (AttrGetObjectRGBColor(PObj, &Color[0], &Color[1], &Color[2])) {
  390.     SetColorRGB(Color);
  391.     }
  392.     else if ((c = AttrGetObjectColor(PObj)) != IP_ATTR_NO_COLOR) {
  393.     SetColorIndex(c);
  394.     }
  395.     else {
  396.     /* Use white as default color: */
  397.     SetColorIndex(IG_IRIT_WHITE);
  398.     }
  399. }
  400.  
  401. /*****************************************************************************
  402. * DESCRIPTION:                                                               M
  403. * Sets the line width to draw the given object, in pixels.             M
  404. *                                                                            *
  405. * PARAMETERS:                                                                M
  406. *   Width:    In pixels of lines to draw with.                               M
  407. *                                                                            *
  408. * RETURN VALUE:                                                              M
  409. *   void                                                                     M
  410. *                                                                            *
  411. * KEYWORDS:                                                                  M
  412. *   IGSetWidthObj                                                            M
  413. *****************************************************************************/
  414. void IGSetWidthObj(int Width)
  415. {
  416.     glLineWidth(IGGlblLineWidth);
  417. }
  418.  
  419. /*****************************************************************************
  420. * DESCRIPTION:                                                               *
  421. * Sets the color according to the given color index.                     *
  422. *                                                                            *
  423. * PARAMETERS:                                                                *
  424. *   color:     Index of color to use. Must be between 0 and IG_MAX_COLOR.    *
  425. *                                                                            *
  426. * RETURN VALUE:                                                              *
  427. *   void                                                                     *
  428. *****************************************************************************/
  429. static void SetColorIndex(int c)
  430. {
  431.     int Color[3];
  432.     static short Colors[IG_MAX_COLOR + 1][3] = {
  433.     { 0,   0,   0   },  /* 0. BLACK */
  434.     { 0,   0,   170 },  /* 1. BLUE */
  435.     { 0,   170, 0   },  /* 2. GREEN */
  436.     { 0,   170, 170 },  /* 3. CYAN */
  437.     { 170, 0,   0   },  /* 4. RED */
  438.     { 170, 0,   170 },  /* 5. MAGENTA */
  439.     { 170, 170, 0   },  /* 6. BROWN */
  440.     { 170, 170, 170 },  /* 7. LIGHTGREY */
  441.     { 85,  85,  85  },  /* 8. DARKGRAY */
  442.     { 85,  85,  255 },  /* 9. LIGHTBLUE */
  443.     { 85,  255, 85  },  /* 10. LIGHTGREEN */
  444.     { 85,  255, 255 },  /* 11. LIGHTCYAN */
  445.     { 255, 85,  85  },  /* 12. LIGHTRED */
  446.     { 255, 85,  255 },  /* 13. LIGHTMAGENTA */
  447.     { 255, 255, 85  },  /* 14. YELLOW */
  448.     { 255, 255, 255 }   /* 15. WHITE */
  449.     };
  450.  
  451.     if (c < 0 || c > IG_MAX_COLOR)
  452.         c = IG_IRIT_WHITE;
  453.  
  454.     Color[0] = Colors[c][0];
  455.     Color[1] = Colors[c][1];
  456.     Color[2] = Colors[c][2];
  457.  
  458.     SetColorRGB(Color);
  459. }
  460.  
  461. /*****************************************************************************
  462. * DESCRIPTION:                                                               *
  463. * Sets the color according to the given RGB values.                 *
  464. *                                                                            *
  465. * PARAMETERS:                                                                *
  466. *   Color:      An RGB vector of integer values between 0 and 255.           *
  467. *                                                                            *
  468. * RETURN VALUE:                                                              *
  469. *   void                                                                     *
  470. *****************************************************************************/
  471. static void SetColorRGB(int Color[3])
  472. {
  473.     int i;
  474.  
  475.     if (IGGlblDrawSolid) {
  476.     GLfloat MatAmbient[4], MatDiffuse[4], MatSpecular[4];
  477.     static GLfloat
  478.         MatShininess[] = { 15.0 };
  479.  
  480.     for (i = 0; i < 3; i++) {
  481.         MatAmbient[i] = 0.2 * Color[0] / 255.0;
  482.         MatDiffuse[i] = 0.4 * Color[i] / 255.0;
  483.         MatSpecular[i] = Color[i] / 255.0;
  484.     }
  485.     MatAmbient[3] = MatDiffuse[3] = MatSpecular[3] = 1.0;
  486.  
  487.     glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, MatAmbient);
  488.     glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MatDiffuse);
  489.     glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, MatSpecular);
  490.     glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, MatShininess);
  491.     }
  492.     else
  493.     glColor3f(Color[0] / 255.0, Color[1] / 255.0, Color[2] / 255.0);
  494.  
  495.     if (IGGlblDepthCue) {
  496.     GLfloat fogColor[4];
  497.  
  498.     for (i = 0; i < 3; i++)
  499.         fogColor[i] = (float) IGGlblBackGroundColor[i] / 255.0;
  500.     fogColor[3] = (float) 1.0;
  501.  
  502.     glEnable(GL_FOG);
  503.     glFogi(GL_FOG_MODE, GL_LINEAR);
  504.     glHint(GL_FOG_HINT, GL_NICEST);
  505.     glFogf(GL_FOG_START, (float) IGGlblZMinClip);
  506.     glFogf(GL_FOG_END, (float) IGGlblZMaxClip);
  507.     glFogfv(GL_FOG_COLOR, fogColor);
  508.     }
  509.     else
  510.     glDisable(GL_FOG);
  511. }
  512.  
  513. /*****************************************************************************
  514. * DESCRIPTION:                                                               M
  515. * Redraw the viewing window.                             M
  516. *                                                                            *
  517. * PARAMETERS:                                                                M
  518. *   none                                                                 M
  519. *                                                                            *
  520. * RETURN VALUE:                                                              M
  521. *   void                                                                 M
  522. *                                                                            *
  523. * KEYWORDS:                                                                  M
  524. *   IGRedrawViewWindow                                                       M
  525. *****************************************************************************/
  526. void IGRedrawViewWindow(void)
  527. {
  528.     RedrawViewWindow(IGhWndView, WM_PAINT, 0);
  529. }
  530.  
  531. /*****************************************************************************
  532. * DESCRIPTION:                                                               M
  533. * Redraw the viewing window.                             M
  534. *                                                                            *
  535. * PARAMETERS:                                                                M
  536. *   hWnd:     Handle on window to draw to.                                   M
  537. *   wMsg:     Event to handle.                                               M
  538. *   wParam:   Some parameters of event.                                      M
  539. *                                                                            *
  540. * RETURN VALUE:                                                              M
  541. *   int:      Window's condition.                                            M
  542. *                                                                            *
  543. * KEYWORDS:                                                                  M
  544. *   RedrawViewWindow                                                         M
  545. *****************************************************************************/
  546. int RedrawViewWindow(HWND hWnd, UINT wMsg, WPARAM wParam)
  547. {
  548.     IPObjectStruct *PObj;
  549.     GLdouble CrntView[16];
  550.     int i, j, k;
  551.     PAINTSTRUCT ps;
  552.  
  553.     switch (wMsg) {
  554.     case WM_CREATE:
  555.         {
  556.         HDC hDC;
  557.         HGLRC hRC;
  558.  
  559.         hDC = GetDC(hWnd);
  560.         bSetupPixelFormat(hDC);
  561.  
  562.         hRC = wglCreateContext(hDC);
  563.         wglMakeCurrent(hDC, hRC);
  564.  
  565.         glClearColor(IGGlblBackGroundColor[0] / 255.0,
  566.                  IGGlblBackGroundColor[1] / 255.0,
  567.                  IGGlblBackGroundColor[2] / 255.0,
  568.                  1.0);
  569.         glClearDepth(1.0);
  570.  
  571.         glMatrixMode(GL_MODELVIEW);
  572.         }
  573.         break;
  574.     case WM_PAINT:
  575.         if (IGCurrenthDC = BeginPaint(hWnd, &ps)) {
  576.         glDrawBuffer(IGGlblDoDoubleBuffer ? GL_BACK : GL_FRONT);
  577.  
  578.         /* Clear viewing area. */
  579.         glClear(GL_COLOR_BUFFER_BIT |
  580.             (IGGlblDrawSolid ? GL_DEPTH_BUFFER_BIT : 0));
  581.  
  582.         /* activate zbuffer only if we are in solid drawing mode. */
  583.         if (IGGlblDrawSolid) {
  584.             static int
  585.             FirstTime = TRUE;
  586.  
  587.             if (FirstTime) {
  588.             static GLfloat
  589.                 Light0Position[4] = { 1.0, 2.0, 10.0, 0.0 },
  590.                 Light1Position[4] = { -5.0, -1.0, -10.0, 0.0 },
  591.                 LightAmbient[] = { 1.0, 1.0, 1.0, 1.0 },
  592.                 LightDiffuse[] = { 1.0, 1.0, 1.0, 1.0 },
  593.                 LightSpecular[] = { 1.0, 1.0, 1.0, 1.0 },
  594.                 LModelAmbient[] = { 0.2, 0.2, 0.2, 1.0 };
  595.             int i;
  596.  
  597.             for (i = 0; i < 4; i++)
  598.                 Light0Position[i] = IGGlblLightSrcPos[i];
  599.  
  600.             glLightfv(GL_LIGHT0, GL_POSITION, Light0Position);
  601.             glLightfv(GL_LIGHT0, GL_AMBIENT, LightAmbient);
  602.             glLightfv(GL_LIGHT0, GL_DIFFUSE, LightDiffuse);
  603.             glLightfv(GL_LIGHT0, GL_SPECULAR, LightSpecular);
  604.             glLightfv(GL_LIGHT1, GL_POSITION, Light1Position);
  605.             glLightfv(GL_LIGHT1, GL_AMBIENT, LightAmbient);
  606.             glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse);
  607.             glLightfv(GL_LIGHT1, GL_SPECULAR, LightSpecular);
  608.  
  609.             glLightModelfv(GL_LIGHT_MODEL_AMBIENT, LModelAmbient);
  610.             glDepthFunc(GL_GREATER);
  611.             glClearDepth(0.0);
  612.  
  613.             FirstTime = FALSE;
  614.             }
  615.  
  616.             glEnable(GL_LIGHTING);
  617.             glEnable(GL_LIGHT0);
  618.             glEnable(GL_DEPTH_TEST);
  619.             glClear(GL_DEPTH_BUFFER_BIT);
  620.         }
  621.         else {
  622.             glDisable(GL_LIGHTING);
  623.             glDisable(GL_LIGHT0);
  624.             glDisable(GL_DEPTH_TEST);
  625.         }
  626.  
  627.         if (IGGlblViewMode == IG_VIEW_PERSPECTIVE) {
  628.             for (i = 0; i < 4; i++)
  629.             for (j = 0; j < 4; j++) {
  630.                 CrntView[i * 4 + j] = 0;
  631.                 for (k = 0; k < 4; k++)
  632.                 CrntView[i * 4 + j] += IritPrsrViewMat[i][k] *
  633.                                IritPrsrPrspMat[k][j];
  634.             }
  635.         }
  636.         else {
  637.             for (i = 0; i < 4; i++)
  638.             for (j = 0; j < 4; j++)
  639.                 CrntView[i * 4 + j] = IritPrsrViewMat[i][j];
  640.         }
  641.  
  642.         glLoadMatrixd(CrntView);
  643.         glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
  644.  
  645.         for (PObj = IGGlblDisplayList; PObj != NULL; PObj = PObj -> Pnext)
  646.             IGDrawObject(PObj);
  647.         glFlush();
  648.  
  649.         if (IGGlblDoDoubleBuffer) {
  650.             HDC hDC2 = wglGetCurrentDC();
  651.  
  652.             SwapBuffers(hDC2);
  653.         }
  654.  
  655.         EndPaint(hWnd, &ps);
  656.         }
  657.         break;
  658.     case WM_QUERYNEWPALETTE:
  659.         {
  660.         HDC     hDC;
  661.  
  662.         if (ghPalette)
  663.         {
  664.             hDC = GetDC(hWnd);
  665.  
  666.             ghpalOld = SelectPalette(hDC, ghPalette, FALSE);
  667.             RealizePalette(hDC);
  668.  
  669.             InvalidateRect(hWnd, NULL, TRUE);
  670.             UpdateWindow(hWnd);
  671.  
  672.             if (ghpalOld)
  673.             SelectPalette(hDC, ghpalOld, FALSE);
  674.  
  675.             ReleaseDC(hWnd, hDC);
  676.  
  677.             return TRUE;
  678.         }
  679.  
  680.         return FALSE;
  681.         }
  682.     case WM_PALETTECHANGED:
  683.         {
  684.         HDC     hDC;
  685.  
  686.         if (ghPalette)
  687.         {
  688.             
  689.             if (wParam != (WPARAM) hWnd)
  690.             {
  691.             hDC = GetDC(hWnd);
  692.  
  693.             ghpalOld = SelectPalette(hDC, ghPalette, FALSE);
  694.             RealizePalette(hDC);
  695.  
  696.             UpdateColors(hDC);
  697.  
  698.             if (ghpalOld)
  699.                 SelectPalette(hDC, ghpalOld, FALSE);
  700.  
  701.             ReleaseDC(hWnd, hDC);
  702.             }
  703.         }
  704.         break;
  705.         }
  706.     }
  707.  
  708.     return FALSE;
  709. }
  710.